package com.idea.editor.lang.c;

import com.idea.editor.lang.LexerTokenizer;
import com.idea.editor.util.DocumentProvider;
import com.idea.editor.util.Flag;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token;

import java.util.ArrayList;

public class CTokenizer extends LexerTokenizer {
    private static CTokenizer _theOne = null;

    public static CTokenizer getInstance() {
        if (_theOne == null) {
            _theOne = new CTokenizer();
        }
        return _theOne;
    }

    @Override
    public ArrayList<Token> tokenize(DocumentProvider hDoc, Flag _abort) {
        CharStream stream = CharStreams.fromString(hDoc.toString());
        CLexer cLexer = new CLexer(stream);
        CommonTokenStream commonToken = new CommonTokenStream(cLexer);
        CParser cParser = new CParser(commonToken);
        cParser.translationUnit();
        return new ArrayList<>(commonToken.getTokens());
    }
}
